package com.his.struts.dao.users;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.his.struts.dao.type.TypeImp;
import com.his.struts.form.DiseaseForm;
import com.his.struts.form.DoctorForm;
import com.his.struts.form.InHisDiseaseForm;
import com.his.struts.form.UserForm;
import com.his.struts.form.ZydiseaseForm;
import com.his.struts.util.DbUtil;

/**
 * 综合
 * 
 * @author liuyan
 * 
 */
public class UsersImp implements UsersDao {
	Logger log = Logger.getLogger(UsersImp.class);
	 DbUtil db = new DbUtil();

	/**
	 * 用户登陆
	 * 
	 * @param name
	 *            姓名
	 * @param pass
	 *            密码
	 * @return 是否成功
	 */
	public boolean login(String name, String pass) {
		boolean ok = false;
		ArrayList sqlList = new ArrayList();
		sqlList.add(name);
		String sql = "select upass from userinfo where uname = ?";
		ResultSet rs = db.executeQuery(sql, sqlList);
		try {
			while (rs.next()) {
				String upass = rs.getString(1);
				if (pass.equals(upass)) {
					ok = true;
				} else {
					ok = false;
				}
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return ok;
	}

	/**
	 * 注册
	 * 
	 * @param users
	 *            注册信息
	 * @return boolean 是否注册成功
	 */
	public boolean regedit(UserForm users) {
		boolean ok = false;
		ArrayList list = new ArrayList();
		list.add(users.getUname());
		list.add(users.getUpass());
		list.add(users.getUemail());
		list.add(users.getSex());
		list.add(users.getUrealname());
		list.add(users.getUaddress());
		list.add(users.getUbirth());
		list.add(users.getUphone());
		list.add("3");
		list.add(users.getUposs());
		String sql = "insert into userinfo (uname,upass,uemail,sex,urealname,uaddress,ubirth,uphone,urole,uposs) values (?,?,?,?,?,?,?,?,?,?)";
		int i = db.executeUpdate(sql, list);
		if (i != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}

	/**
	 * 医生登陆
	 * 
	 * @param name
	 *            姓名
	 * @param pass
	 *            密码
	 * @return 是否成功
	 */
	public boolean docLogin(String name, String pass) {
		boolean ok = false;
		ArrayList sqlList = new ArrayList();
		sqlList.add(name);
		String sql = "select dpass from doctorinfo where dname = ?";
		ResultSet rs = db.executeQuery(sql, sqlList);
		try {
			while (rs.next()) {
				String upass = rs.getString(1);
				if (pass.equals(upass)) {
					ok = true;
				} else {
					ok = false;
				}
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return ok;
	}

	/**
	 * 取医生首页信息
	 * 
	 * @param ysname
	 *            医生名
	 * @return DoctorForm
	 */
	public DoctorForm docIndex(String name) {
		boolean ok = false;
		ArrayList sqlList = new ArrayList();
		sqlList.add(name);
		String sql = "select * from doctorinfo where dname = ?";
		ResultSet rs = db.executeQuery(sql, sqlList);
		DoctorForm docinfo = new DoctorForm();
		try {
			if (rs.next()) {
				docinfo.setDocid(rs.getInt("did"));
				docinfo.setDocname(rs.getString("dname"));
				docinfo.setDocpass(rs.getString("dpass"));
				docinfo.setDocpicture(rs.getString("dpicture"));
				docinfo.setDocsex(rs.getString("dsex"));
				docinfo.setDocemail(rs.getString("demail"));
				docinfo.setDocposname(rs.getString("dposname"));
				docinfo.setDocattend(rs.getString("attend"));
				docinfo.setDocphone(rs.getString("dphone"));
				docinfo.setDocinfo(rs.getString("dinfo"));
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return docinfo;
	}

	/**
	 * 取用户首页信息
	 * 
	 * @param username
	 *            用户名
	 * @return UserForm
	 */
	public UserForm userIndex(String username) {
		boolean ok = false;
		ArrayList sqlList = new ArrayList();
		sqlList.add(username);
		String sql = "select * from userinfo where uname = ?";
		ResultSet rs = db.executeQuery(sql, sqlList);
		UserForm userinfo = new UserForm();
		try {
			if (rs.next()) {
				userinfo.setUid(rs.getInt("uid"));
				userinfo.setUname(rs.getString("uname"));
				userinfo.setUpass(rs.getString("upass"));
				userinfo.setUemail(rs.getString("uemail"));
				userinfo.setUpicture(rs.getString("upicture"));
				userinfo.setSex(rs.getString("sex"));
				userinfo.setUrealname(rs.getString("urealname"));
				userinfo.setUaddress(rs.getString("uaddress"));
				userinfo.setUbirth(rs.getString("ubirth"));
				userinfo.setUphone(rs.getString("uphone"));
				userinfo.setUrole(rs.getString("urole"));
				userinfo.setUposs(rs.getString("uposs"));
				int age = getAge(rs.getDate("ubirth"));
				userinfo.setAge(age);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return userinfo;
	}

	/**
	 * 根据生日算出年龄
	 * 
	 * @param ubirth
	 *            生日
	 * @return 年龄
	 */
	public int getAge(Date ubirth) {
		int age = 0;
		Date date = new Date();
		int nowyear = date.getYear();
		int birthyear = ubirth.getYear();
		age = nowyear - birthyear - 1;
		int birthmonth = ubirth.getMonth();
		int nowmonth = date.getMonth();
		int birthday = ubirth.getDate();
		int nowday = date.getDate();
		if (birthmonth < nowmonth) {
			age = age + 1;
		} else if (birthmonth == nowmonth && birthday >= nowday) {
			age = age + 1;
		}
		if(age < 0){
			age = 0;
		}
		db.closeAll();
		return age;
	}

	/**
	 * 取一个用户所有门诊病历
	 * 
	 * @param uid
	 *            用户ID
	 * @return List 集合
	 */
	public List<DiseaseForm> allDisease(int uid) {
		ArrayList sqlList = new ArrayList();
		sqlList.add(uid);
		String sql = "select * from casehis where uid = ?";
		ResultSet rs = db.executeQuery(sql, sqlList);
		List<DiseaseForm> list = new ArrayList<DiseaseForm>();
		try {
			while (rs.next()) {
				DiseaseForm df = new DiseaseForm();
				df.setCaseid(rs.getInt(1));
				df.setUname(rs.getString(2));
				df.setFacname(rs.getString(3));
				df.setCasetime(rs.getString(4));
				df.setMainsym(rs.getString(5));
				df.setConsultbody(rs.getString(6));
				df.setAsssonsult(rs.getString(7));
				df.setPridiagnose(rs.getString(8));
				df.setDealmethod(rs.getString(9));
				df.setReciperecode(rs.getString(10));
				df.setRecdoctor(rs.getString(11));
				list.add(df);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		}
		db.closeAll();
		return list;
	}

	/**
	 * 取用户ID
	 * 
	 * @param name
	 *            用户名
	 * @return ID
	 */
	public int getuid(String name) {
		ArrayList sqlList = new ArrayList();
		sqlList.add(name);
		String sql = "select uid from userinfo where uname = ?";
		int i = 0;
		ResultSet rs = db.executeQuery(sql, sqlList);
		try {
			while (rs.next()) {
				i = rs.getInt(1);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		}
		db.closeAll();
		return i;
	}

	/**
	 * 取一个门诊病历的信息
	 * 
	 * @param caseid
	 *            门诊号
	 * @return DiseaseForm
	 */
	public DiseaseForm oneDisease(int caseid) {
		ArrayList sqlList = new ArrayList();
		sqlList.add(caseid);
		String sql = "select * from casehis where caseid = ?";
		ResultSet rs = db.executeQuery(sql, sqlList);
		DiseaseForm df = new DiseaseForm();
		try {
			while (rs.next()) {
				df.setCaseid(rs.getInt(1));
				df.setUname(rs.getString(2));
				df.setFacname(rs.getString(3));
				df.setCasetime(rs.getString(4));
				df.setMainsym(rs.getString(5));
				df.setConsultbody(rs.getString(6));
				df.setAsssonsult(rs.getString(7));
				df.setPridiagnose(rs.getString(8));
				df.setDealmethod(rs.getString(9));
				df.setReciperecode(rs.getString(10));
				df.setRecdoctor(rs.getString(11));
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return df;
	}

	/**
	 * 检查用户名
	 * 
	 * @param userName
	 *            用户名
	 * @return Boolean 是否存在
	 */
	public boolean checkUserName(String userName) {
		boolean ok = false;
		ArrayList sqlList = new ArrayList();
		sqlList.add(userName);
		String sql = "select * from userinfo where uname = ?";
		ResultSet rs = db.executeQuery(sql, sqlList);
		try {
			if (rs.next()) {
				ok = true;
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return ok;
	}

	/**
	 * 取医生ID
	 * 
	 * @param name
	 *            用户名
	 * @return ID
	 */
	public int getDocid(String name) {
		ArrayList sqlList = new ArrayList();
		sqlList.add(name);
		String sql = "select did from doctorinfo where dname = ?";
		int i = 0;
		ResultSet rs = db.executeQuery(sql, sqlList);
		try {
			while (rs.next()) {
				i = rs.getInt(1);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return i;
	}

	/**
	 * 封装医生看过的所有病人
	 * 
	 * @param rs
	 *            医生看过的所有病人
	 * @return 医生看过的病人
	 */
	public List<DiseaseForm> getBean(ResultSet rs) {
		List<DiseaseForm> list = new ArrayList<DiseaseForm>();
		try {
			while (rs.next()) {
				DiseaseForm df = new DiseaseForm();
				df.setCaseid(rs.getInt(1));
				df.setUname(rs.getString(2));
				df.setFacname(rs.getString(3));
				df.setCasetime(rs.getString(4));
				df.setMainsym(rs.getString(5));
				df.setConsultbody(rs.getString(6));
				df.setAsssonsult(rs.getString(7));
				df.setPridiagnose(rs.getString(8));
				df.setDealmethod(rs.getString(9));
				df.setReciperecode(rs.getString(10));
				df.setRecdoctor(rs.getString(11));
				list.add(df);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return list;
	}

	/**
	 * 更新病人资料
	 * 
	 * @param diseaseForm
	 *            需要更新的所有数据
	 * @return 更新条数
	 */
	public int updateDisease(DiseaseForm diseaseForm) {
		String sql = "update casehis set facname=?,casetime=?,"
				+ "mainsym=?,consultbody=?,asssonsult=?,pridiagnose=?,"
				+ "dealmethod=?,reciperecode=?,recdoctor=? where caseid=?";
		ArrayList list = new ArrayList();
		list.add(diseaseForm.getFacname());
		list.add(diseaseForm.getCasetime());
		list.add(diseaseForm.getMainsym());
		list.add(diseaseForm.getConsultbody());
		list.add(diseaseForm.getAsssonsult());
		list.add(diseaseForm.getPridiagnose());
		list.add(diseaseForm.getDealmethod());
		list.add(diseaseForm.getReciperecode());
		list.add(diseaseForm.getRecdoctor());
		list.add(diseaseForm.getCaseid());

		int n = db.executeUpdate(sql, list);
		db.closeAll();
		return n;
	}

	/**
	 * 得到所有用户
	 * 
	 * @return 所有用户
	 */
	public List<UserForm> getUserBean(ResultSet rs) {
		List list = new ArrayList();
		try {
			while (rs.next()) {
				UserForm user = new UserForm();
				user.setUid(rs.getInt("uid"));
				user.setUname(rs.getString("uname"));
				user.setUpass(rs.getString("upass"));
				user.setUpicture(rs.getString("upicture"));
				user.setUemail(rs.getString("uemail"));
				user.setSex(rs.getString("sex"));
				user.setUrealname(rs.getString("urealname"));
				user.setUaddress(rs.getString("uaddress"));
				user.setUbirth(rs.getString("ubirth"));
				user.setUphone(rs.getString("uphone"));
				user.setUrole(rs.getString("urole"));
				user.setUposs(rs.getString("uposs"));
				list.add(user);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return list;
	}

	/**
	 * 取用户住院病历
	 * 
	 * @param rs
	 *            ResultSet
	 * @return List<InHisDiseaseForm>
	 */
	public List<InHisDiseaseForm> getAllInHisDisease(int uid) {
		List<InHisDiseaseForm> list = new ArrayList<InHisDiseaseForm>();
		String sql = "select * from inhis where uid = ?";
		ArrayList sqlList = new ArrayList();
		sqlList.add(uid);
		ResultSet rs = db.executeQuery(sql, sqlList);
		try {
			while (rs.next()) {
				InHisDiseaseForm idf = new InHisDiseaseForm();
				int ihid = rs.getInt("ihid");
				idf.setIhid(ihid);
				idf.setUid(uid);
				idf.setInhdate(rs.getString("inhdate"));
				idf.setOuthdate(rs.getString("outhdate"));
				idf.setInhdiatnlose(rs.getString("inhdiatnlose"));
				List preList = this.getAllPrescript(ihid);
				idf.setPrescript(preList);
				list.add(idf);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return list;
	}

	public List getAllPrescript(int ihid) {
		List list = new ArrayList();
		String sql = "select mdid from prescript where ihid = ?";
		ArrayList sqlList = new ArrayList();
		sqlList.add(ihid);
		ResultSet rs = db.executeQuery(sql, sqlList);
		try {
			while (rs.next()) {
				int mdid = rs.getInt("mdid");
				ArrayList sqlList2 = new ArrayList();
				sqlList2.add(mdid);
				String sql1 = "select medicine from medicine where mdid = ?";
				ResultSet rs1 = db.executeQuery(sql1, sqlList2);
				while (rs1.next()) {
					String medicine = rs1.getString(1);
					list.add(medicine);
				}
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return list;
	}

	/**
	 * 删除用户
	 * 
	 * @param uid
	 *            用户id
	 */
	public void deleteUser(int uid) {
		ArrayList sqlList = new ArrayList();
		sqlList.add(uid);
		String sql = "delete from userinfo where uid = ?";
		db.executeUpdate(sql, sqlList);
		db.closeAll();
	}

	/**
	 * 删除病例
	 * 
	 * @param id
	 *            病例id
	 */
	public void deleteDesease(int id) {
		ArrayList sqlList = new ArrayList();
		sqlList.add(id);
		String sql = "delete from casehis where caseid =?";
		db.executeUpdate(sql, sqlList);
		db.closeAll();
	}

	/**
	 * 管理员登陆
	 */
	public UserForm adminLogin(String name) {
		UserForm userForm = new UserForm();
		boolean ok = false;
		String sql = "select upass,urole from userinfo where uname = ?";
		ArrayList sqlList = new ArrayList();
		sqlList.add(name);
		ResultSet rs = db.executeQuery(sql, sqlList);
		try {
			while (rs.next()) {
				userForm.setUpass(rs.getString(1));
				userForm.setUrole(rs.getString(2));
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return userForm;
	}

	public List<InHisDiseaseForm> getAllInHisDisease(ResultSet rs) {
		List<InHisDiseaseForm> list = new ArrayList<InHisDiseaseForm>();
		try {
			while (rs.next()) {
				InHisDiseaseForm idf = new InHisDiseaseForm();
				int ihid = rs.getInt("ihid");
				idf.setIhid(ihid);
				idf.setUid(rs.getInt("uid"));
				idf.setInhdate(rs.getString("inhdate"));
				idf.setOuthdate(rs.getString("outhdate"));
				idf.setInhdiatnlose(rs.getString("inhdiatnlose"));
				List preList = this.getAllPrescript(ihid);
				idf.setPrescript(preList);
				list.add(idf);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return list;
	}


	/**
	 * 删除中医病历
	 * 
	 * @param zyid
	 *            中医病历号
	 */
	public void deleteInHidDesease(int id) {
		String sql = "delete from inhis where ihid =" + id;
		db.executeUpdate(sql, null);
		db.closeAll();
	}

	/**
	 * 所有中医病历
	 * 
	 * @param uid
	 *            用户id
	 * @return List<ZydiseaseForm>
	 */
	public List<ZydiseaseForm> allzyDisease(ResultSet rs) {
		List<ZydiseaseForm> list = new ArrayList<ZydiseaseForm>();
		try {
			while (rs.next()) {
				ZydiseaseForm zydf = new ZydiseaseForm();
				zydf.setUid(rs.getInt("uid"));
				zydf.setZyzs(rs.getString("zyzs"));
				zydf.setZyid(rs.getInt("zyid"));
				zydf.setZybs(rs.getString("zybs"));
				zydf.setZycl(rs.getString("zycl"));
				zydf.setZyyf(getZyyf(rs.getInt("zyid")));
				zydf.setZyzd(rs.getString("zyzd"));
				list.add(zydf);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return list;
	}

	public List getZyyf(int zyid) {
		List list = new ArrayList();
		String sql = "select mid from zyyf where zyid = " + zyid;
		ResultSet rs = db.executeQuery(sql, null);
		try {
			while (rs.next()) {
				int mid = rs.getInt(1);
				String sql1 = "select medicine from medicine where mdid = ?";
				ArrayList l = new ArrayList();
				l.add(mid);
				ResultSet rs1 = db.executeQuery(sql1, l);
				while (rs1.next()) {
					String medicine = rs1.getString(1);
					list.add(medicine);
				}
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return list;
	}

	public void deletezyDisease(int zyid) {
		String sql = "delete from zybl where zyid = " + zyid;
		int i = db.executeUpdate(sql, null);
		db.closeAll();
	}

	/**
	 * 得到所有医生
	 * 
	 * @return List<DoctorForm>医生集合
	 */
	public List<DoctorForm> getDocBean(ResultSet rs) {
		List<DoctorForm> list = new ArrayList<DoctorForm>();
		DoctorForm form = null;
		try {
			while (rs.next()) {
				form = new DoctorForm();
				int docid = rs.getInt("did");
				String docname = rs.getString("dname");
				String docposname = rs.getString("dposname");
				String docattend = rs.getString("attend");
				String docphone = rs.getString("dphone");
				String docpass = rs.getString("dpass");
				String docsex = rs.getString("dsex");
				String demail = rs.getString("demail");
				String dinfo = rs.getString("dinfo");
				form.setDocid(docid);
				form.setDocattend(docattend);
				form.setDocemail(demail);
				form.setDocinfo(dinfo);
				form.setDocname(docname);
				form.setDocpass(docpass);
				form.setDocphone(docphone);
				form.setDocposname(docposname);
				form.setDocsex(docsex);
				list.add(form);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}

		return list;
	}

	/**
	 * 删除医生
	 * 
	 * @param did医生id
	 * @return boolean
	 */
	public boolean delDoc(int did) {
		boolean ok = false;
		ArrayList list = new ArrayList();
		String sql = "delete from doctorinfo where did=?";
		list.add(did);
		int i = db.executeUpdate(sql, list);
		if (i != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}

	/**
	 * 添加医生
	 * 
	 * @param form
	 * @return boolean
	 */
	public boolean addDoc(DoctorForm form) {
		boolean ok = false;
		String sql = "insert into doctorinfo(dname,dpass,dsex,demail,dposname,attend,dphone,dinfo) values(?,?,?,?,?,?,?,?)";
		ArrayList list = new ArrayList();
		list.add(form.getDocname());
		list.add(form.getDocpass());
		list.add(form.getDocsex());
		list.add(form.getDocemail());
		list.add(form.getDocposname());
		list.add(form.getDocattend());
		list.add(form.getDocphone());
		list.add(form.getDocinfo());
		int i = db.executeUpdate(sql, list);
		if (i != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}

	/**
	 * 更新医生
	 * 
	 * @param form
	 * @return boolean
	 */
	public boolean updateDoc(DoctorForm form) {
		boolean ok = false;
		String sql = "update doctorinfo set dname=?,dpass=?,dsex=?,demail=?," +
				"dposname=?,attend=?,dphone=?,dinfo=? where did=?";
		ArrayList list = new ArrayList();
		list.add(form.getDocname());
		list.add(form.getDocpass());
		list.add(form.getDocsex());
		list.add(form.getDocemail());
		list.add(form.getDocposname());
		list.add(form.getDocattend());
		list.add(form.getDocphone());
		list.add(form.getDocinfo());
		list.add(form.getDocid());
		int i = db.executeUpdate(sql, list);
		if (i != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}

	/**
	 * 更新医生资料
	 * 
	 * @param doctorInfo
	 *            医生资料
	 */
	public void updateDoctorInfo(DoctorForm doctorInfo) {
		String sql = "update doctorInfo set dpass=?, "
				+ "dsex=?, demail=?, dphone=?,dinfo=? where dname=?";
		ArrayList list = new ArrayList();
		list.add(doctorInfo.getDocpass());
		list.add(doctorInfo.getDocsex());
		list.add(doctorInfo.getDocemail());
		list.add(doctorInfo.getDocphone());
		list.add(doctorInfo.getDocinfo());
		if(doctorInfo.getDocpicture() !="" && doctorInfo.getDocpicture() != null){
			sql = "update doctorInfo set dpass=?, "
				+ "dsex=?, demail=?, dphone=?,dinfo=?,dpicture=? where dname=?";
			list.add(doctorInfo.getDocpicture());
		}
		list.add(doctorInfo.getDocname());
		db.executeUpdate(sql, list);
		db.closeAll();

	}

	/**
	 * 更新用户资料
	 * 
	 * @param doctorInfo
	 *            医生资料
	 */
	public void updateUserInfo(UserForm userInfo) {
		String sql = "update userInfo set upass=?, "
				+ "sex=?, uemail=?, uphone=?,ubirth=? where uname=?";
		ArrayList list = new ArrayList();
		list.add(userInfo.getUpass());
		list.add(userInfo.getSex());
		list.add(userInfo.getUemail());
		list.add(userInfo.getUphone());
		list.add(userInfo.getUbirth());
		if(userInfo.getUpicture() !="" && userInfo.getUpicture() !=null){
			sql = "update userInfo set upass=?, "
				+ "sex=?, uemail=?, uphone=?,ubirth=?, upicture=? where uname=?";
			list.add(userInfo.getUpicture());
		}
		list.add(userInfo.getUname());
		db.executeUpdate(sql, list);
		db.closeAll();
	}

	/**
	 * 得到所有医生
	 * 
	 * @return List<DoctorForm>医生集合
	 */
	public List<DoctorForm> allDoc() {
		List<DoctorForm> list = new ArrayList<DoctorForm>();
		String sql = "select *  from doctorinfo left join kebie on kb_id=attend";
		DoctorForm form = null;
		ResultSet rs = db.executeQuery(sql, null);
		try {
			while (rs.next()) {
				form = new DoctorForm();
				int docid = rs.getInt("did");
				String docname = rs.getString("dname");
				String docposname = rs.getString("dposname");
				String docattend = rs.getString("attend");
				String docphone = rs.getString("dphone");
				String docpass = rs.getString("dpass");
				String docsex = rs.getString("dsex");
				String demail = rs.getString("demail");
				String dinfo = rs.getString("dinfo");
				form.setDocid(docid);
				form.setDocattend(docattend);
				form.setDocemail(demail);
				form.setDocinfo(dinfo);
				form.setDocname(docname);
				form.setDocpass(docpass);
				form.setDocphone(docphone);
				form.setDocposname(docposname);
				form.setDocsex(docsex);
				list.add(form);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}

		return list;
	}
	/**
	 *添加一个门诊病历
	 * @param uid 用户ID
	 * @return List 集合
	 */
	public boolean addDisease(DiseaseForm df){
		boolean ok = false;
			ArrayList list = new ArrayList();
			list.add(df.getUid());
			list.add(df.getFacname());
			list.add(df.getCasetime());
			list.add(df.getMainsym());
			list.add(df.getConsultbody());
			list.add(df.getAsssonsult());
			list.add(df.getPridiagnose());
			list.add(df.getDealmethod());
			list.add(df.getReciperecode());
			list.add(df.getRecdoctor());
			String sql = "insert into casehis (uid,facname,casetime,mainsym,consultbody,asssonsult,pridiagnose,dealmethod,reciperecode,recdoctor) values (?,?,?,?,?,?,?,?,?,?)";
			int i = db.executeUpdate(sql, list);
			if(i == 1){
				ok = true;
			}
			db.closeAll();
		return ok;
	}
	/**
	 * 添加住院病历
	 * @param idf InHisDiseaseForm
	 * @return 是否添加成功
	 */
	public boolean addInHisDisease(InHisDiseaseForm idf,int k) {
		boolean ok = false;
		String sql = "insert into inhis (uid,inhdate,outhdate,inhdiatnlose) values (?,?,?,?)";
		if(k == 2){
			sql = "insert into inhis (ihid,uid,inhdate,outhdate,inhdiatnlose) values (?,?,?,?,?)";
		}
		ArrayList list = new ArrayList();
		if(k == 2){
			list.add(idf.getIhid());
		}
		list.add(idf.getUid());
		list.add(idf.getInhdate());
		list.add(idf.getOuthdate());
		list.add(idf.getInhdiatnlose());
		int i = db.executeUpdate(sql, list);
		List ywlist = idf.getPrescript();
		ArrayList list1 = new ArrayList();
		String sql1 = "select ihid from inhis where uid = ? order by ihid desc";
		list1.add(idf.getUid());
		ResultSet rs = db.executeQuery(sql1, list1);
		int ihid = 0;
		try {
			while(rs.next()){
				ihid = rs.getInt(1);
				break;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			db.closeAll();
		}
		
		
		for (int j = 0; j < ywlist.size(); j++) {
			String ywname = (String)ywlist.get(j);
			if(k == 2){
				ihid = idf.getIhid();
			}
			int m = checkywid(ihid,ywname,2);
			if(m == 0){
				break;
			}
		}
		if(i == 1){
			ok = true;
		}
		return ok;
	}
	/**
	 * 添加中医病历
	 * @param zyid 中医病历号
	 */
	public boolean addzyDisease(ZydiseaseForm zdf,int k){
		boolean ok = false;
		String sql = "insert into zybl (uid,zytime,zydocid,zyzs,zybs,zyzd,zycl) values (?,?,?,?,?,?,?)";
		if(k == 2){
			sql = "insert into zybl (zyid,uid,zytime,zydocid,zyzs,zybs,zyzd,zycl) values (?,?,?,?,?,?,?,?)";
		}
		ArrayList list = new ArrayList();
		if(k == 2){
			list.add(zdf.getZyid());
		}
		list.add(zdf.getUid());
		list.add(zdf.getZytime());
		list.add(getDocid(zdf.getZydoctor()));
		list.add(zdf.getZyzs());
		list.add(zdf.getZybs());
		list.add(zdf.getZyzd());
		list.add(zdf.getZycl());
		int i = db.executeUpdate(sql, list);
		ArrayList list1 = new ArrayList();
		String sql1 = "select zyid from zybl where uid = ? order by zyid desc";
		list1.add(zdf.getUid());
		ResultSet rs = db.executeQuery(sql1, list1);
		int zyid = 0;
		try {
			while(rs.next()){
				zyid = rs.getInt(1);
				break;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			db.closeAll();
		}
		
		List ywlist = zdf.getZyyf();
		
		for (int j = 0; j < ywlist.size(); j++) {
			String ywname = (String)ywlist.get(j);
			if(k == 2){
				zyid = zdf.getZyid();
			}
			int m = checkywid(zyid,ywname,1);
			if(m == 0){
				break;
			}
		}
		if(i == 1){
			ok = true;
		}
		return ok;
	}
	/**
	 * 查找药物ID
	 * @param idf InHisDiseaseForm
	 * @return 是否添加成功
	 */
	public  int checkywid(int id,String ywname,int k) {
		int ywid = 0;
		String sql = "select mdid from medicine where medicine = ?";
		ArrayList list = new ArrayList();
		list.add(ywname);
		ResultSet rs = db.executeQuery(sql, list);
			try {
				while(rs.next()){
				ywid = rs.getInt("mdid");
				}
 			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				db.closeAll();
			}
 		ArrayList list1 = new ArrayList();
 		String sql1 = null;
 		if(k == 1){
 			sql1 = "insert into zyyf (zyid,mid) values (?,?)";
 		}else if(k == 2){
 			sql1 = "insert into prescript (ihid,mdid) values (?,?)";
 		}
 		list1.add(id);
 		list1.add(ywid);
 		int i =db.executeUpdate(sql1, list1);
 		db.closeAll();
		return i;
	}
	
	/**
	 * 得到一个住院病历
	 * @param ihid 住院号
	 * @return InHisDiseaseForm
	 */
	public InHisDiseaseForm getInHisDisease(int ihid) {
		String sql = "select * from inhis where ihid = ?";
		InHisDiseaseForm ihdf = null;
		ArrayList list = new ArrayList();
		list.add(ihid);
		ResultSet rs = db.executeQuery(sql, list);
		if(rs != null){
			try {
				while(rs.next()){
					ihdf = new InHisDiseaseForm();
					ihdf.setIhid(ihid);
					ihdf.setInhdate(rs.getString("inhdate"));
					ihdf.setInhdiatnlose(rs.getString("inhdiatnlose"));
					ihdf.setOuthdate(rs.getString("outhdate"));
					ihdf.setPrescript(this.getAllPrescript(ihid));
					ihdf.setUid(rs.getInt("uid"));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return ihdf;
	}
	
	
	
	/**
	 * 得到一个中医病历
	 * @param ihid 住院号
	 * @return InHisDiseaseForm
	 */
	public ZydiseaseForm getZyDisease(int zyid) {
		String sql = "select * from zybl where zyid = ?";
		ZydiseaseForm zdf = null;
		ArrayList list = new ArrayList();
		list.add(zyid);
		ResultSet rs = db.executeQuery(sql, list);
		if(rs != null){
			try {
				while(rs.next()){
					zdf = new ZydiseaseForm();
					zdf.setUid(rs.getInt("uid"));
					zdf.setZybs(rs.getString("zybs"));
					zdf.setZycl(rs.getString("zycl"));
					zdf.setZydoctor(this.getDocname(rs.getInt("zydocid")));
					zdf.setZyid(zyid);
					zdf.setZytime(rs.getString("zytime"));
					zdf.setZyyf(this.getZyyf(zyid));
					zdf.setZyzs(rs.getString("zyzs"));
					zdf.setZyzd(rs.getString("zyzd"));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return zdf;
	}

	/**
	 * 编辑一个住院病历
	 * @param InHisDiseaseForm 
	 * @return boolean
	 */
	public boolean editInHisDisease(InHisDiseaseForm ihdf) {
		this.deleteInHidDesease(ihdf.getIhid());
		boolean ok = this.addInHisDisease(ihdf,2);
		return ok;
	}
	/**
	 * 取医生名字
	 * @param docid 医生ID
	 * @return 医生名
	 */
	public String getDocname(int docid) {
		String sql = "select dname from doctorinfo where did = ?";
		ArrayList list = new ArrayList();
		list.add(docid);
		ResultSet rs = db.executeQuery(sql, list);
		String name = null;
		try {
			while(rs.next()){
				name = rs.getString("dname");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return name;
	}
	/*
	 * 编辑中医病例
	 * 
	 */
	public boolean editZyDisease(ZydiseaseForm zdf) {
		this.deletezyDisease(zdf.getZyid());
		boolean ok = this.addzyDisease(zdf,2);
		return ok;
	}
}
